package com.LFLWZ.controller;


import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.POJO.ClassroomCourse;
import com.LFLWZ.beans.POJO.Comments;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.beans.Vo.CommentsByReplyVo;
import com.LFLWZ.beans.Vo.CommentsVo;
import com.LFLWZ.service.ClassroomCourseService;
import com.LFLWZ.service.CommentsService;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("comments/")
public class CommentsController {

    @Reference
    private CommentsService commentsService;
    @Reference
    private ClassroomCourseService classroomCourseService;

    @PostMapping("addComments")
    public Dto addComments(@RequestBody Comments comments){
        Dto dto=null;
        if (EmptyUtils.isEmpty(comments))
            dto= DtoUtil.returnError(4001,"参数为空");
       if (comments.getType().equals("lesson")){
           System.out.println(comments.getUid());
           System.out.println(comments.getAid());
           Comments comments1=new Comments("lesson",comments.getUid(),comments.getAid());
           List<CommentsByReplyVo> list= commentsService.queryAll(comments1);
           if (EmptyUtils.isNotEmpty(list)){
               dto=DtoUtil.returnError(4002,"已经评价过了");
               return dto;
           }
       }
        Comments c= commentsService.insert(comments);
        if (EmptyUtils.isEmpty(c))
            dto=DtoUtil.returnError(4001,"查询失败");
        else {
            dto = DtoUtil.returnSuccessData(c);
            if (c.getType().equals("lesson")){
                ClassroomCourseVo classroomCourseVo= classroomCourseService.queryById(comments.getAid());
                Dto list =selById(comments.getAid(),comments.getType());
                List<CommentsByReplyVo> list1=(List<CommentsByReplyVo>) list.getData();
                int num=0;
                for (CommentsByReplyVo comments1:list1){
                    num=num+comments1.getScore();
                }
                Integer score= num / list1.size();
                classroomCourseVo.setScore(score);
                classroomCourseService.updateScore(classroomCourseVo);
            }
        }
        return dto;
    }

    /**
     * 根据被评论id和评论类型  查询评论
     * @param aid
     * @param type
     * @return
     */
    @GetMapping("selByAid/{aid}/{type}")
    public Dto selById(@PathVariable Integer aid,@PathVariable String type){
        Dto dto=null;
        if (EmptyUtils.isEmpty(aid))
            dto=DtoUtil.returnError(4001,"aid为空");
        Comments c1=new Comments();
        if(aid!=0){
            c1.setAid(aid);
        }
        c1.setType(type);
        List<CommentsByReplyVo> list= commentsService.queryAll(c1);

        if (EmptyUtils.isEmpty(list))
            dto=DtoUtil.returnError(4001,"查询失败");
        else{
            for (int i=0;i<list.size();i++) {
                Comments comments1 = new Comments();
                comments1.setAid(list.get(i).getId());
                List<Comments> comments2 = commentsService.selReply(comments1);
                if (EmptyUtils.isNotEmpty(comments2)) {
                    list.get(i).setCommentsByReply(comments2);
                }
            }
        }
            dto=DtoUtil.returnSuccessData(list);
        return dto;
    }

    @GetMapping("deById/{id}")
    public Dto deById(@PathVariable Integer id){
        Dto dto=null;
        if (id==null) {
            dto = DtoUtil.returnError(4001, "参数错误");
            return dto;
        }
        Boolean bool= commentsService.deleteById(id);
        if (bool)
            dto=DtoUtil.returnSuccessData(bool);
        else
            dto=DtoUtil.returnError(4002,"错误");
        return dto;
    }
}
